/**
 * formdata请求*/
import axios from 'axios'
import { getToken } from '@/utils/auth'
import qs from 'qs'

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  timeout: 20000 // request timeout
})

service.interceptors.request.use(
  config => {
    const token = getToken()
    if (token) {
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      config.headers['Authorization'] = 'Bearer ' + token
      config.transformRequest = [function(data) {
        data = qs.stringify(data)
        return data
      }]
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.log('err' + error)
    return Promise.reject(error)
  }
)
export default service
